<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 5/9/24
 * Time: 3:25 PM
 */

namespace app\adminapi\logic\jump;


use app\common\model\jump\Jump;
use app\common\logic\BaseLogic;
use app\common\model\quickapp\Quickapp;

class JumpLogic extends BaseLogic
{

    /**
     * 详情
     *
     * @return array
     */
    public static function detail($id)
    {
        $jump = Jump::field('id, type, admin_id, platform, title, jump_url, status, create_time, update_time')
            ->where('id', '=', $id)
            ->findOrEmpty()
            ->toArray();
        $jump['enterprise_wechat'] = Quickapp::where('id',$jump['platform'])->value('enterprise_wechat')??0;
        return $jump;
    }


    /**
     * 新增
     *
     * @return bool
     */
    public static function add($params)
    {
        $platform = $params['platform'] ?? 0;
        $adminId  = $params['admin_id'] ?? 0;
        $title    = $params['title'] ?? '';
        $type     = $params['type'] ?? 0;
        $enterprise_wechat     = $params['enterprise_wechat'] ?? 0;
        $jumpUrl  = $params['jump_url'] ?? '';

        if (!$platform || !$jumpUrl || !$type) {

            return false;
        }

        $jump = new Jump();

        $jump->type        = $type;
        $jump->platform    = $platform;
        $jump->admin_id    = $adminId;
        $jump->title       = $title;
        $jump->jump_url    = $jumpUrl;
        $jump->status      = 1;
        $jump->create_time = time();
        $jump->update_time = time();
        //如果创建的是企微跳转链接，更新企微弹框范围
        Quickapp::where('id',$platform)->update(['enterprise_wechat'=>$enterprise_wechat]);
        return $jump->save();
    }


    /**
     * 编辑
     *
     * @return bool
     */
    public static function edit($params)
    {
        $id       = $params['id'] ?? 0;
        $platform = $params['platform'] ?? 0;
        $adminId  = $params['admin_id'] ?? 0;
        $title    = $params['title'] ?? 0;
        $type     = $params['type'] ?? 0;
        $jumpUrl  = $params['jump_url'] ?? '';
        $enterprise_wechat     = $params['enterprise_wechat'] ?? 0;
        if (!$id || !$platform || !$jumpUrl || !$type) {

            return false;
        }
        $jump = Jump::where('id', '=', $id)->findOrEmpty();
        //如果创建的是企微跳转链接，更新企微弹框范围

        $onlineJump = Jump::where(['status'=>1])->order('id desc')->findOrEmpty();
//        if (!$onlineJump->isEmpty()&&$onlineJump->id==$id){
            Quickapp::where('id',$jump->platform)->update(['enterprise_wechat'=>$enterprise_wechat]);
//        }

        if ($jump->isEmpty()) {

            return false;
        }

        $jump->platform    = $platform;
        $jump->admin_id    = $adminId;
        $jump->title       = $title;
        $jump->type        = $type;
        $jump->jump_url    = $jumpUrl;
        $jump->update_time = time();

        return $jump->save();
    }


    /**
     * 禁用 /启用
     *
     * @return bool
     */
    public static function updateStatus($params)
    {
        $id     = $params['id'] ?? 0;
        $status = $params['status'] ?? 1;

        $jump = Jump::where('id', '=', $id)->findOrEmpty();
        if ($jump->isEmpty()) {

            return false;
        }
        $jump->status      = $status;
        $jump->update_time = time();

        return $jump->save();
    }


    /**
     * 删除
     *
     * @return bool
     */
    public static function delete($id)
    {
        return Jump::destroy($id);
    }
}
